package cn.kbblog.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




/**
 * 读取properties资源文件工具类
 * 
 */
public class PropertiesUtil implements AutoCloseable{
	
	private static final String DEFAULT_PROP = "/config.properties";
	private static Logger LOG = LoggerFactory.getLogger(PropertiesUtil.class);
	
	public static Properties getProperties(String propName){
		Properties props = new Properties();
		try (InputStream in = PropertiesUtil.class.getResourceAsStream(propName)){
			props.load(in);
		} catch (IOException e) {
			LOG.error(e.getMessage(),e);
		}
		return props;
	}

	/**
	 * 读取app.properties资源文件的工具类
	 * 
	 * @param key
	 * @return
	 */
	public static String getProps(String key) {
		return getProperties(DEFAULT_PROP).getProperty(key);
	}
	
	
    /**
     * 根据key,获取属性值
     *
     * @param key
     * @param defaultValue 默认值
     * @return
     */
    public static String getWithDefault(String key, String defaultValue){
        return getProperties(DEFAULT_PROP).getProperty(key,defaultValue);
    }


	@Override
	public void close() throws IOException {
	}
	
}
